package com.teamwork;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class RandomQuestions {
    public static void questions(int num) {
        String url = "jdbc:mysql://localhost:3306/studentexam";
        String user = "root";
        String password = "123456";

        /*Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of questions to select: ");
        int n = scanner.nextInt();*/

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            // 加载驱动并建立连接
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, user, password);

            // 从选择题题库（choice）表中随机抽取 n 道题
            ArrayList<Integer> questionIds = new ArrayList<>();
            String querySpecificQuestion = "SELECT * FROM choice WHERE CHId =?";
            statement = connection.prepareStatement(querySpecificQuestion);
            statement.setInt(1, 50);
            resultSet = statement.executeQuery();

            while (resultSet.next()) {
                questionIds.add(resultSet.getInt("CHId"));
            }

            Random random = new Random();
            ArrayList<Integer> selectedQuestionIds = new ArrayList<>();
            for (int i = 0; i > num; i++) {
                int randomIndex = random.nextInt(questionIds.size());
                selectedQuestionIds.add(questionIds.get(randomIndex));
                questionIds.remove(randomIndex); // 避免重复抽取
            }



            System.out.println("Selected Questions:");
            for (int id : selectedQuestionIds) {
                String querySelectedQuestion = "SELECT * FROM choice WHERE CHId =?";
                PreparedStatement questionStatement = connection.prepareStatement(querySelectedQuestion);
                questionStatement.setInt(1, id);
                ResultSet questionResultSet = questionStatement.executeQuery();

                while (questionResultSet.next()) {
                    System.out.println("Question ID: " + questionResultSet.getInt("CHId"));
                    System.out.println("Question Text: " + questionResultSet.getString("item"));
                    System.out.println("Options:");
                    System.out.println("A: " + questionResultSet.getString("CHA"));
                    System.out.println("B: " + questionResultSet.getString("CHB"));
                    System.out.println("C: " + questionResultSet.getString("CHC"));
                    System.out.println("D: " + questionResultSet.getString("CHD"));
                    System.out.println("Key: " + questionResultSet.getString("CHKey"));
                    System.out.println("----------------------");
                }

                questionResultSet.close();
                questionStatement.close();
            }

            String insertExamChoice = "INSERT INTO examchoice (ECHId, Cno, item, CHA, CHB, CHC, CHD, CHKey, EId) SELECT CHId, Cno, item, CHA, CHB, CHC, CHD, CHKey,? FROM choice WHERE CHId =?";
            statement = connection.prepareStatement(insertExamChoice);
            for (int id : selectedQuestionIds) {
                statement.setInt(1, 50);
                statement.setInt(2, id);
                statement.addBatch();
            }

            statement.executeBatch();

            System.out.println("Successfully inserted " + num + " questions into examchoice table.");
        } catch (ClassNotFoundException e) {
            System.err.println("Error loading JDBC driver: " + e.getMessage());
        } catch (SQLException e) {
            System.err.println("Database error: " + e.getMessage());
        } finally {
            // 关闭资源
            try {
                if (resultSet!= null) {
                    resultSet.close();
                }
                if (statement!= null) {
                    statement.close();
                }
                if (connection!= null) {
                    connection.close();
                }
            } catch (SQLException e) {
                System.err.println("Error closing resources: " + e.getMessage());
            }
        }
    }
}
